$(function() {
	init();
});
var oPage;
var _uri = 'freight_analysis';
var _uri_back = 'freight_analysis_inquiry';
function init(){
        getHandler('common', {fun:'limit',code:'25'}, function(val){
			$('#buttonBox').html(val);
	},goBack);
	postHandler(_uri, {fun:'list'}, function(val){
		$('.fancyTable').html(val);
		postHandler(_uri, {fun:'titles'}, function(val){
			initTable();
			initPrintPopup(val);
			initPopupWindow();
			setTimeout('initFixedHeaderTable()',500);
                        setTimeout('reviseWidth()',500);
			initDateRange();
		},goBack);
	},goBack);
}
function reviseWidth(){
    var tds=$('.fht-fixed-body .fht-tbody tbody tr:eq(0) td');
    for(var i=1; i<tds.length;i++){
         var w=$(tds[i]).width();
         $('.fht-fixed-body .fht-thead  thead tr:eq(1) th').eq(i-1).width(w);
    }
    // console.log("-$('.fht-thead').height():"+(-$('.fht-fixed-body .fht-thead thead').height())+", height:"+$('.fht-fixed-body .fht-thead').height());
    $('#myTable05').css('margin-top',-$('.fht-fixed-body .fht-thead thead').height());
    $('.fht-fixed-column .fht-thead .fht-table tr th').height($('.fht-fixed-body .fht-thead thead').height()-10);
    togglemask(false);
}
function initDateRange(){
	postHandler(_uri, {fun:'date_range'}, function(val){
		$('h1').eq(0).html('运费分析-'+val+'</span>');
	});
}
function goBack(){
	//location.href = _uri_back+'.html';
	return false;
}
function initTable(){
	var oChks = $('.checkbox_box_w').find('input:checkbox');
	var oTable = $('#myTable05');
	var _thead = oTable.find('thead');
	var _tbody = oTable.find('tbody');
	var oTh1 = _thead.find('tr:eq(0)').find('th');
	var oTh2 = _thead.find('tr:eq(1)').find('th');
	var oTr = _tbody.find('tr');
	var col = 0;
	oTh1.each(function(idx){
		$(this).addClass('t1_'+idx);
		var colspan = $(this).attr('colspan') || 0;
		for(var i=0;i<colspan;i++){
			oTh2.eq(col++).addClass('t2_'+idx+'_'+i);
		}
	});
	oTh2.each(function(idx){
		
		//$(this).addClass('t2_'+idx);
	});
	oTr.each(function(idx1){
		$(this).find('td').each(function(idx2){
			$(this).addClass('t_'+idx2);
		});
	});
}
function chkboxOk(){
	var oChks = $('.checkbox_box_w').find('input:checkbox');
	oPage = $('#myTable05').clone();
	var _thead = oPage.find('thead');
	var _tbody = oPage.find('tbody');
	var oTh1 = _thead.find('tr:eq(0)').find('th');
	var oTh2 = _thead.find('tr:eq(1)').find('th');
	var oTr = _tbody.find('tr');
	//alert(oTh1.eq(0).attr('colspan'));
	oChks.each(function(idx){
		var chk = $(this)[0].checked;
		var tok = $(this)[0].id.split('_');
		if(tok.length==4 && chk==false){
			oPage.find('.t_'+tok[3]).each(function(){$(this).remove();});
			//oPage.find('.t2_'+tok[3]).each(function(){$(this).remove();});
			oPage.find('.t2_'+tok[1]+'_'+tok[2]).each(function(){$(this).remove();});
			var colspan = oPage.find('.t1_'+tok[1]).attr('colspan');
			oPage.find('.t1_'+tok[1]).attr('colspan', colspan-1);
			 if(colspan<=1) oPage.find('.t1_'+tok[1]).remove();
			
		}
	});
	openPrintPage();
	return false;
}
function openPrintPage(){
	var html = oPage.html();
	var title = $('title').html();
	var h1 = $('h1').html();
	var _html = '<html><body><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'+
		'<title>'+title+'</title><script type="text/javascript" src="layout/head.js"></script>'+
		'<style type="text/css" media="print">.no_print{display: none;}</style></head>'+
		'<body><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><p class="small">'+
		h1+'</p></td><td style="text-align: right;">'+
		'<input class="no_print button_" type="button" value="打印" onclick="print();window.close();"/><input class="no_print button_" type="button" value="返回" onclick="window.close()"/></td>'+
		'</tr></table><div class=""><table class="fancyTable" id="myTable05" border="0" cellspacing="0" cellpadding="0">'+
		html+'</table></div></body></html>';
	var win = window.open('','newwindow', 'toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=n o, status=no');
	win.document.write(_html);
	win.document.close();
	oPage.remove();
}
function initPrintPopup(val){
	var oWin = $('.checkbox_box_w');
	var html = '<div><label><input name="checkbox" type="checkbox" id="" checked="checked"><strong style="color:#00aacc;">全部</strong></label></div><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk()" type="button" value="确定" />';
	oWin.html(html);
	var div = oWin.find('.checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	for(var i=0;i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked"><strong>'+t1+'</strong></label>';
		if(tok2.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var t2 = tok3[j];
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked">'+t2+'</label></div>';
				idx++;
			}
			_html += '</div>';
		}
		if(i>0)
		_div.html(_html).appendTo(div);
	}
	setTimeout('initChkbox()',500);
}
function initFixedHeaderTable(){
	$('.fancyTable').fixedHeaderTable({ width:'1000px', altClass: 'odd', footer: false, fixedColumns: 1});
}

function download(){
	location.href = _uri+'?fun=xls';
	return false;
}